/**
 * 
 */
package org.dragonfire.editor;

import java.util.HashMap;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * @author Darth Android
 * @date Oct 7, 2007
 */
public class AttributeManager {

	private static HashMap<String, TextAttribute> attributes = new HashMap<String, TextAttribute>(20);

	public static TextAttribute getAttribute(String name) {
		return attributes.get(name);
	}

	public static void addAttribute(String name, TextAttribute attribute) {
		attributes.put(name, attribute);
	}

	public static void addAttribute(String name, Color fg) {
		addAttribute(name, new TextAttribute(fg));
	}

	public static void addAttribute(String name, Color fg, int style) {
		addAttribute(name, new TextAttribute(fg, null, style));
	}

	public static void addAttribute(String name, int red, int green, int blue) {
		addAttribute(name, new Color(Display.getCurrent(), red, green, blue));
	}

	public static void addAttribute(String name, int red, int green, int blue, int style) {
		addAttribute(name, new Color(Display.getCurrent(), red, green, blue), style);
	}

	public static void addAttribute(String name, Color fg, Color bg, int style) {
		addAttribute(name, new TextAttribute(fg, bg, style));
	}

	public static void addAttribute(String name, int fgRed, int fgGreen, int fgBlue, int bgRed, int bgGreen,
			int bgBlue, int style) {
		addAttribute(name, new Color(Display.getCurrent(), new RGB(fgRed, fgGreen, fgBlue)), new Color(Display
				.getCurrent(), new RGB(bgRed, bgGreen, bgBlue)), style);
	}

	public static void addDefaultAttribute(String name, Color fg, Color bg, int style) {
		if (!attributes.containsKey(name)) {
			addAttribute(name, fg, bg, style);
		}
	}

	public static void addDefaultAttribute(String name, Color fg, int style) {
		if (!attributes.containsKey(name)) {
			addAttribute(name, fg, style);
		}
	}

	public static void addDefaultAttribute(String name, Color fg) {
		if (!attributes.containsKey(name)) {
			addAttribute(name, fg);
		}
	}

	public static void addDefaultAttribute(String name, int fgRed, int fgGreen, int fgBlue, int bgRed, int bgGreen,
			int bgBlue, int style) {
		if (!attributes.containsKey(name)) {
			addAttribute(name, fgRed, fgGreen, fgBlue, bgRed, bgGreen, bgBlue, style);
		}
	}

	public static void addDefaultAttribute(String name, int fgRed, int fgGreen, int fgBlue, int style) {
		if (!attributes.containsKey(name)) {
			addAttribute(name, fgRed, fgGreen, fgBlue, style);
		}
	}

	public static void addDefaultAttribute(String name, int fgRed, int fgGreen, int fgBlue) {
		addDefaultAttribute(name, fgRed, fgGreen, fgBlue, SWT.NONE);
	}

	public static IToken createToken(TextAttribute style) {
		return new Token(style);
	}

	public static IToken getToken(String name) {
		return createToken(getAttribute(name));
	}
}
